<?
	include_once('header.php');

	include_once('sidebar.php');

	$edit = $_GET['edit'];
	$offset = $_GET['offset'];
	$limit = $_GET['limit'];
	$action = $_GET['action'];
	if(!$offset) { $offset = "0"; }
	if(!$limit) { $limit = "20"; }
?>

		
		<div id="main">
			
<?
	if($action == "batch_delete") {
?>
			<h2>Permanently Delete Posts</h2>
			<br class="clear" />
<?
		if(!isset($_POST['delete_check'])) {
			$batch_delete_id = $_POST['batch_delete_id'];
			$delete_count = count($batch_delete_id);
			$row_count = 1;
			foreach($batch_delete_id as $delete_id) {
				$id = $delete_id;
				$ids .= $id . " ";
				$the_post = drizzo_get_the_trash($delete_id);
				$the_title = $the_post['title'];
				if($row_count < $delete_count) {
					$the_title = $the_title . ", ";
				}
				$titles .= $the_title;
				$row_count++;
			}
?>
			<p class="alert">Are you sure you want to permanently delete <? echo $titles; ?> and any associated previous Warehouse revisions and/or  comments?</p>
			<form action="trashheap.php?action=batch_delete" method="post" name="deleter" id="deleter">
				<input type="hidden" name="delete_check" value="1"></input>
				<input type="hidden" name="delete_ids" value="<? echo $ids; ?>"></input>
				<p><input type="button" onclick="history.go(-1)" value="No" class="submit"></input><input type="submit" name="submit" value="Yes" class="submit"></input></p>
			</form>
<?
		} elseif(isset($_POST['delete_check'])) {
			$delete_ids = explode(" ", rtrim($_POST['delete_ids']));
			
			foreach($delete_ids as $delete_id) {
				$delete_post = drizzo_get_the_trash($delete_id);
				$delete_query = @mysql_query("DELETE FROM `$drizzo_trashheap` WHERE `ID`='$delete_id'");
				if(!mysql_errno()) {
					$delete_comments_query = @mysql_query("DELETE FROM `$drizzo_comments` WHERE `post_id`='$delete_post[old_id]'");
					$delete_comments_number = @mysql_affected_rows();
					$delete_revisions_query = @mysql_query("DELETE FROM `$drizzo_warehouse` WHERE `old_id`='$delete_post[old_id]'");
					$delete_revisions_count = @mysql_affected_rows();
					if($delete_comments_number > 0 && $delete_revisions_count > 0) {
						echo "<p class=\"trash\">" . $delete_post['title'] . ", its $delete_revisions_number Revisions and $delete_comments_number Comments have been deleted.</p>";
					} elseif($delete_comments_number > 0 && $delete_revisions_count <= 0) {
						echo "<p class=\"trash\">" . $delete_post['title'] . " and its $delete_revisions_number Revisions have been deleted.</p>";
					} elseif($delete_comments_number <= 0 && $delete_revisions_count > 0) {
						echo "<p class=\"trash\">" . $delete_post['title'] . " and $delete_comments_number Comments have been deleted.</p>";
					} else {
						echo "<p class=\"trash\">" . $delete_post['title'] . " has been deleted.</p>";
					}
				} else {
					echo "<p class=\"alert\">" . $delete_post['title'] . " could not be deleted.<br />" . mysql_error() ."</p>";
				}
			}
		} else {
		}
	} // End Batch Delete

		if($action == "batch_restore") {
	?>
				<h2>Restore Posts</h2>
	<?
			if(!isset($_POST['restore_check'])) {
				$batch_restore_id = $_POST['batch_restore_id'];
				$restore_count = count($batch_restore_id);
				$row_count = 1;
				foreach($batch_restore_id as $restore_id) {
					$id = $restore_id;
					$ids .= $id . " ";
					$the_post = drizzo_get_the_trash($restore_id);
					$the_title = $the_post['title'];
					if($row_count < $restore_count) {
						$the_title = $the_title . ", ";
					}
					$titles .= $the_title;
					$row_count++;
				}
	?>
				<p clas="restore">Are you sure you want to restore <? echo $titles; ?>?</p>
				<form action="trashheap.php?action=batch_restore" method="post" name="restorer" id="restorer">
					<input type="hidden" name="restore_check" value="1"></input>
					<input type="hidden" name="restore_ids" value="<? echo $ids; ?>"></input>
					<p><input type="button" onclick="history.go(-1)" value="No" class="submit"></input><input type="submit" name="submit" value="Yes" class="submit"></input></p>
				</form>
	<?
			} elseif(isset($_POST['restore_check'])) {
				$restore_ids = explode(" ", rtrim($_POST['restore_ids']));

				foreach($restore_ids as $restore_id) {
					$the_post = drizzo_get_the_trash($restore_id);
					
					$id = $the_post['ID'];
					$author = $the_post['author'];
					$type = $the_post['type'];
					$status = $the_post['status'];
					$page_parent = $the_post['page_parent'];
					$page_order = $the_post['page_order'];
					$title = drizzo_db_wrap($the_post['title']);
					$content = drizzo_db_wrap($the_post['content']);
					$tags = drizzo_db_wrap($the_post['tags']);
					$comment_status = $the_post['comment_status'];
					$edit_id = $the_post['edited_by'];
					$edited_date = $the_post['edited_date_raw'];
					$date = $the_post['posted_date_raw'];
					$permalink_name = drizzo_db_wrap($the_post['permalink_name']);
					$menu_name = drizzo_db_wrap($the_post['menu_name']);
					$lat = $the_post['lat'];
					$lon = $the_post['lon'];
					$old_id = $the_post['old_id'];

					drizzo_db_connect();

					$restore_query = @mysql_query("INSERT INTO `$drizzo_posts` SET `ID`='$old_id',`author`='$author',`type`='$type',`status`='$status',`page_parent`='$page_parent',`page_order`='$page_order',`title`='$title',`content`='$content',`tags`='$tags',`comment_status`='$comment_status',`edited_by`='$edit_id',`posted_date`=$date,`permalink_name`='$permalink_name',`menu_name`='$menu_name',`lat`='$lat',`lon`='$lon',`edited_date`=NOW()");

					if(!mysql_errno()) {
						$delete_from_trashheap = @mysql_query("DELETE FROM `$drizzo_trashheap` WHERE `ID`='$id' LIMIT 1");
						echo "<p class=\"trash\">" . $the_post['title'] . " has been restored.</p>";
					} else {
						echo "<p class=\"alert\">" . $the_post['title'] . " could not be restored.<br />" . mysql_error() ."</p>";
					}
				}
			} else {
			}
		} // End Batch Restore

	if(!$action) { // Default View

		drizzo_db_connect();
	
		$prev_offset = ($offset + $limit);
		$next_offset = abs(($offset - $limit));
		$trash_posts = drizzo_get_the_trashheap($limit, $offset);
		$total_count = count($trash_posts);
		if($prev_offset < $total_count) { $show_prev = "true"; }
		if($offset !=0) { $show_next = "true"; }
	
?>
			<h2>In the TrashHeap</h2>
<?
		if(($show_prev == "true") || ($show_next == "true")) {  // Previous & Next top navigation
			echo "<p class=\"right\">";
		}
		if($show_prev == "true") {
			echo "<a href=\"trashheap.php?offset=" . $prev_offset . "\" title=\"Earlier Entries\">&laquo; Earlier Entries</a>";
		}
		if(($show_prev == "true") && ($show_next == "true")) {
			echo " | ";
		}
		if($show_next == "true") {
			echo "<a href=\"trashheap.php?offset=" . $next_offset . "\" title=\"Later Entries\">Later Entries &raquo;</a>";
		}
		if(($show_prev == "true") || ($show_next == "true")) {
			echo "</p>";
		}
?>			
		<form id="batch_delete" name="batch_delete" method="post" action="">
			<table id="trashheap_posts">
				<tr id="first">
					<th>ID</th><th>Title</th><th>Type</th><th>Posted</th><th>Comments</th><th>Author</th><th class="center">Delete</th><th class="center">Restore</th>
				</tr>
<?
			$alt_count = 0;
		if($total_count > 0) {
			foreach($trash_posts as $trash) { // Parse through the posts array and output table rows
				extract($trash);
				if(drizzo_post_permissions_check()) {
					if ($alt_count%2) {
						echo "<tr class=\"alt\">\n";
					} else {
						echo "<tr>\n";
					}
					echo "\t<td class=\"center\" width=\"1\">" . $ID . "</td><td>" . $title . "</td><td class=\"center\" width=\"1\">" . $type . "</td><td>" . $posted_date . "</td><td class=\"center\" width=\"1\">" . $comments_count . "</td><td class=\"center\" width=\"1\">" . $author . "</td><td class=\"center\" width=\"1\"><input type=\"checkbox\" name=\"batch_delete_id[]\" value=\"" . $ID . "\" class=\"checkbox\"></input></td><td class=\"center\" width=\"1\"><input type=\"checkbox\" name=\"batch_restore_id[]\" value=\"" . $ID . "\" class=\"checkbox\"></input></td>\n";
					echo "</tr>\n";
					$alt_count++;
				} else {
					
				}
			} // End parse through the posts array
			
		} else {
			if ($alt_count%2) {
				echo "<tr class=\"alt\">\n";
			} else {
				echo "<tr>\n";
			}
			echo "\t<td colspan=\"10\">No Posts were found.</td>\n";
			echo "</tr>\n";
			$alt_count++;
		}
		if ($alt_count%2) {
			echo "<tr class=\"alt\">\n";
		} else {
			echo "<tr>\n";
		}
?>					
					<td colspan="6"></td><td class="center"><a href="javascript:void(0);" onclick="SetAllCheckBoxes('batch_delete', 'batch_delete_id[]', 'selected')">Select All</a></td><td class="center"><a href="javascript:void(0);" onclick="SetAllCheckBoxes('batch_delete', 'batch_restore_id[]', 'selected')">Select All</a></td>
				</tr>
<?
		if ($alt_count%2) {
			echo "<tr class=\"alt\">\n";
		} else {
			echo "<tr>\n";
		}
?>
					<td colspan="6"></td><td class="center"><a href="javascript:void(0);" onclick="SetAllCheckBoxes('batch_delete', 'batch_delete_id[]', '')">Deselect All</a></td><td class="center"><a href="javascript:void(0);" onclick="SetAllCheckBoxes('batch_delete', 'batch_restore_id[]', '')">Deselect All</a></td>
				</tr>
			</table>
			<p class="right"><input type="submit" name="delete" onclick="document.batch_delete.action = 'trashheap.php?action=batch_delete'" value="Delete" class="submit"></input><input type="submit" name="restore" onclick="document.batch_delete.action = 'trashheap.php?action=batch_restore'" value="Restore" class="submit"></input></p>
		</form>

<?
			if(($show_prev == "true") || ($show_next == "true")) { // Bottom Previous & Next
				echo "<p class=\"right\">";
			}
			if($show_prev == "true") {
				echo "<a href=\"trashheap.php?offset=" . $prev_offset . "\" title=\"Earlier Entries\">&laquo; Earlier Entries</a>";
			}
			if(($show_prev == "true") && ($show_next == "true")) {
				echo " | ";
			}
			if($show_next == "true") {
				echo "<a href=\"trashheap.php?offset=" . $next_offset . "\" title=\"Later Entries\">Later Entries &raquo;</a>";
			}
			if(($show_prev == "true") || ($show_next == "true")) {
				echo "</p>";
			}
		} // End Default View
?>
			<br class="clear" />

		</div>
<?
	include_once('footer.php');
?>
